// RealPlay Ltd  copyrights.
// This program is Open Source AsIs
// You may use it for all purposes, including commercial
// But!!!
// If you use any part of this code, you must state clearly
// both in your source file and in the Help|about of your program,
// that the origin program or code was built by RealPlay Ltd

// ***********************************************************************/
// *                                                                     */
// *  This file is the FloodFill (when depth<>0) unit of "Paint" Program */
// *                                                                     */
// ***********************************************************************/

unit UPFloodfill;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls,
  Buttons, ExtCtrls, Mask;

type
  TFBinDepthDlg = class(TForm)
    OKBtn: TButton;
    CancelBtn: TButton;
    Bevel1: TBevel;
    CdepE: TMaskEdit;
    StaticText1: TStaticText;
    procedure OKBtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FBinDepthDlg: TFBinDepthDlg;
  Depth: integer = 0;
procedure Myfloodfill(x, y: integer; startC, endC: TColor; bm: Tbitmap;
  h: integer = 0);

implementation

uses UPMain;
{$R *.dfm}
// ************************************************************************

procedure Ifloodfill(x, y: integer; startC, endC: TColor; bm: Tbitmap;
  h: integer = 0);
Var
  PixC: TColor;
begin
  PixC := bm.Canvas.Pixels[x, y];
  if PixC = endC then
    exit;
  if PixC <> startC then
    if (abs((PixC and $FF) - (startC and $FF)) > h) or
      (abs((PixC and $FF00) - (startC and $FF00)) > (h shl 8)) or
      (abs((PixC and $FF0000) - (startC and $FF0000)) > (h shl 16)) then
      exit;
  bm.Canvas.Pixels[x, y] := endC;
  if x > 0 then
    Ifloodfill(x - 1, y, startC, endC, bm, h);
  if x < bm.width - 1 then
    Ifloodfill(x + 1, y, startC, endC, bm, h);
  if y < bm.height - 1 then
    Ifloodfill(x, y + 1, startC, endC, bm, h);
  if y > 0 then
    Ifloodfill(x, y - 1, startC, endC, bm, h);
end;

procedure Myfloodfill(x, y: integer; startC, endC: TColor; bm: Tbitmap;
  h: integer = 0);
begin
  if h = 0 then
  begin
    bm.Canvas.Brush.Color := endC;
    bm.Canvas.FloodFill(x, y, startC, fsSurface);
  end
  else
    Ifloodfill(x, y, startC, endC, bm, h);
end;

procedure TFBinDepthDlg.OKBtnClick(Sender: TObject);
begin
  Depth := strtoint(CdepE.Text);
  FPaint.SBar.Panels[5].Text := ' D=' + inttostr(Depth);
end;

end.
